/*
 * TestProcessExec.java  0.4.0 / Oct 25, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.nunit; // NOPMD

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

/**
 * TestEnvironment.execProcess() Test.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public final class TestProcessExec {

// Package-Private Static Final Fields. ----------------------------------------

  static final String STRING_TEST_PASSED = "Exec Test Passed.";
  static final String STRING_TEST_FAILED = "Exec Test Failed.";
  static final String STRING_TEST = "test";

// Private Constructor. --------------------------------------------------------

 /**
  * Class has no instances.
  */
  private TestProcessExec() {
    // empty constructor body
  }

// Public Static Methods. ------------------------------------------------------

  /**
   * Returns <tt>true</tt> if "exec test" can be run; otherwise <tt>false</tt>.
   *
   * @return <tt>true</tt> if "exec test" can be run; otherwise <tt>false</tt>
   */
  public static boolean testProcessExec() {
    File dir = new File(System.getProperty("user.dir"));
    BufferedReader isr = null;
    try {
      String command = TestEnvironment.getLaunchCommand() +
                       TestProcessExec.class.getName() +
                       TestEnvironment.STRING_SPACE + STRING_TEST;
      System.out.print("Executing: "); // NOPMD
      System.out.println(command); // NOPMD
      System.out.print("in folder: "); // NOPMD
      System.out.println(dir); // NOPMD
      Process jvm = TestEnvironment.execProcess(command, dir);
      if (jvm.waitFor() == 0) {
        isr = new BufferedReader(new InputStreamReader(jvm.getInputStream()));
        return STRING_TEST_PASSED.equals(isr.readLine()); // NOFB
      }
      System.out.print("Exit value: "); // NOPMD
      System.out.println(jvm.exitValue()); // NOPMD
      isr = new BufferedReader(new InputStreamReader(jvm.getErrorStream()));
      for (String nl = isr.readLine(); nl !=  null; nl = isr.readLine()) {
        System.out.println(nl); // NOPMD
      }
    } catch (Throwable t) {
      t.printStackTrace(); // NOPMD
    } finally {
      TestEnvironment.closeReader(isr);
    }
    return false;
  }

  /**
   * Main method.
   *
   * @param args the arguments
   */
  public static void main(String[] args) {
    if ((args != null) && (args.length == 1) &&
        STRING_TEST.equalsIgnoreCase(args[0])) {
      System.out.println(STRING_TEST_PASSED); // NOPMD
    } else {
      if (testProcessExec()) {
        System.out.println(STRING_TEST_PASSED); // NOPMD
        System.exit(0); // NOPMD
      } else {
        System.out.println(STRING_TEST_FAILED); // NOPMD
        System.exit(-1); // NOPMD
      }
    }
  }

}
